<?php
namespace app\admin\controller;

use app\common\model\CustomerCase as CustomerCaseModel;
use app\common\model\Product as ProductModel;
use app\common\model\ProductData as ProductDataModel;
use think\Request;
use app\common\controller\Base;
use app\common\service\Product as ProductService;
use app\common\service\ProductData as ProductDataService;
use app\common\tool\Tool;

class ProductData extends Base{

    public function index(Request $request){
        $id = $request -> param('id',0,'intval');

        $productDataService = new ProductDataService();
        $productData = $productDataService ->allByProductId($id);

        $productService = new ProductService();
        $product = $productService ->find($id);


        return view('',['productData'=>$productData,'product'=>$product]);
    }


    /**
     * content 的数据格式 [["content"=>['标题','内容','标题','内容'],"src"=>"",'tpl'=>'模板标志'],[...],[...]]
     * @param Request $request
     * @return \think\response\View
     */
    public function editContent(Request $request){

        $id = $request->param('id',0,'intval');
        $productDataService = new ProductDataService();
        $productData = $productDataService->find($id);

        $productData->content = unserialize($productData->content);
        $content = $productData->content;
        if(is_array($content)){
            foreach($content as $key => $val ){
                foreach($content[$key]['content'] as $k => $v ){
                    $content[$key]['content'][$k] = str_replace("<br />","\n",$v);
                }
            }
            $productData->content = $content;
        }


        $product = ProductModel::find($productData->productid);

        $tool = new Tool();

        return view('',['productData'=>$productData,'product'=>$product,'tool'=>$tool]);
    }

    /**
     * 这个只有更新 没有新建
     * @param Request $request
     */
    public function updateContent(Request $request){
        $productDataId = $request->param("productDataId",0,'intval');
        $content = $request->param('content');
        if(!$productDataId){
            $this->error("非法来源");
        }
        $productData = ProductDataModel::find($productDataId);
        $content = json_decode($content,true);
        foreach($content as $key => $val ){
            $content[$key]['content'] = str_replace("\n","<br />",$content[$key]['content']);
        }

        if($content){
            $content = serialize($content);
        }
        $productData->content = $content;
        $productData->save();
        $this->error("操作成功");
    }

    /**
     * 纯粹的一个编辑页面 编辑完成从后台
     */
    public function addItem(){

        return view();
    }

    public function editItem(){

        return view();
    }

    public function addSku(Request $request){
        $productId = $request->param('productId',0,'intval');
        if(!$productId){
            echo "非法访问";exit;
        }

        //查询产品是否存在
        $productService = new ProductService();
        $product = $productService->find($productId);
        if(!$product){
            echo "非法访问";exit;
        }

        return view('',['productId'=>$productId]);
    }

    public function createSku(Request $request){
        $sku = $request->param("sku",'','trim');
        $productId = $request->param('productId',0,'intval');

        if(!$sku){
            $this->error("规格必须填写");
        }

        $data = [];
        $data['sku'] = $sku;
        $data['productid'] = $productId;

        $res = ProductDataModel::where('sku',$sku)->where('productid',$productId)->find();
        if($res){
            $this->error("该名称已经存在了");
        }

        $productDataService = new ProductDataService();
        $productDataService ->insert($data);

        $this->success('操作成功');
    }

    public function editSku(Request $request){
        $productId = $request->param('productId',0,'intval');

        $id = $request->param('id',0,'intval');

        if(!$id || !$productId){
            echo "非法访问";exit;
        }

        $productData = ProductDataModel::where('id',$id)->where('productid',$productId)->find();
        if(!$productData) {
            echo "该规格不存在";exit;
        }

        return view('',['productId'=>$productId,'id'=>$id,'productData'=>$productData]);
    }


    public function updateSku(Request $request){
        $id = $request->param('id',0,'intval');
        $sku = $request->param("sku",'','trim');
        $productId = $request->param('productId',0,'intval');

        if(!$sku){
            $this->error("规格必须填写");
        }

        $data = [];
        $data['sku'] = $sku;
        $data['productid'] = $productId;

        $res = ProductDataModel::where('sku',$sku)->where('productid',$productId)->where('id','<>',$id)->find();
        if($res){
            $this->error("该名称已经存在了");
        }

        ProductDataModel::update($data,['id'=>$id]);

        $this->success('操作成功');
    }



    public function images(Request $request){

        $id = $request->param('id',0,'intval');
        $productData = ProductDataModel::where('id',$id)->find();
        $images = unserialize($productData->images);

        $product = ProductModel::find($productData->productid);

        return view('',['id'=>$id,'images'=>$images,'product'=>$product]);
    }

    public function imagesCreate(Request $request){
        if(!$request->isPost()){
            $this->error("非法渠道访问");
        }
        $skuid = $request->param('skuid',0,'intval');
        if(!$skuid){
            $this->error("非法渠道访问");
        }

        $images = $request->only('images');
        if(!$images){
            $images['images'] = [];
        }
        $images = serialize($images['images']);


        ProductDataModel::update(['images'=>$images],['id'=>$skuid]);
        $this->success("更新成功");
    }


    public function parameter(Request $request){
        $id = $request->param('id',0,'intval');

        $productData = ProductDataModel::where('id',$id)->find();
        $parameter = $productData->parameter;
        $product = ProductModel::find($productData->productid);

        return view('',['id'=>$id,'parameter'=>$parameter,'product'=>$product]);
    }


    public function parameterUpdate(Request $request){
        if(!$request->isPost()){
            $this->error("非法渠道访问");
        }

        $skuid = $request->param('skuid',0,'intval');
        if(!$skuid){
            $this->error("非法渠道访问");
        }

        $parameter = $request->param('parameter','','trim');

        ProductDataModel::update(['parameter'=>$parameter],['id'=>$skuid]);
        $this->success("更新成功");
    }


    public function video(Request $request){
        $id = $request->param('id',0,'intval');

        $productData = ProductDataModel::where('id',$id)->find();
        $video = $productData->video;
        $product = ProductModel::find($productData->productid);

        return view('',['id'=>$id,'video'=>$video,'product'=>$product]);
    }

    public function videoUpdate(Request $request){
        if(!$request->isPost()){
            $this->error("非法渠道访问");
        }

        $skuid = $request->param('skuid',0,'intval');
        if(!$skuid){
            $this->error("非法渠道访问");
        }

        $video = $request->param('video','','trim');

        ProductDataModel::update(['video'=>$video],['id'=>$skuid]);
        $this->success("更新成功");
    }

    public function cases(Request $request){
        $id = $request->param('id',0,'intval');

        $productData = ProductDataModel::where('id',$id)->find();
        $cases = $productData->case;
        $cases = unserialize($cases);
        if($cases){
            $cases = CustomerCaseModel::whereIn('id',$cases)->select();
        }
        $product = ProductModel::find($productData->productid);

        return view('',['id'=>$id,'cases'=>$cases,'product'=>$product,'productData'=>$productData]);

    }


    public function caseList(){

        $cases = CustomerCaseModel::order('id desc')->paginate(20);

        return view('',['cases'=>$cases]);
    }

    public function getCase(Request $request){
        $id = $request->param('id',0,'intval');
        $data = CustomerCaseModel::find($id);

        $skuid = $request -> param('skuid',0,'intval');
        $productData = ProductDataModel::find($skuid);
        if(!$productData){
            $this->error("非法渠道访问");
        }
        $cases = unserialize($productData->case);
        if(!$cases){
            $cases = [];
        }
        if(!in_array($id,$cases)){
            array_push($cases,$id);
        }
        $productData->case = serialize($cases);
        $productData->save();

        return json($data);
    }


    public function cancelCase(Request $request){
        $id = $request->param('id',0,'intval');
        $skuid = $request->param('skuid',0,'intval');

        $productData = ProductDataModel::find($skuid);
        if(!$productData){
            $this->error("非法渠道访问");
        }

        $cases = unserialize($productData->case);
        if(!$cases){
            $this->success("取消成功");
        }
        foreach($cases as $key => $val ){
            if($val == $id){
                unset($cases[$key]);
            }
        }

        $productData->case = serialize($cases);
        $productData->save();

        $this->success("取消成功");
    }


    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法渠道访问");
        }
        $id = $request -> param('id');
        ProductDataModel::destroy($id);
        $this->success("删除成功");
    }

}






